﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Modules
{
    public partial class HoaDonItem : DevExpress.XtraEditors.XtraForm
    {
        string sErr = "";
        int idThem = 45;
        int idSua = 46;
        int idXoa = 47;
        void SetPermission()
        {
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = (frmMain)this.Parent.Parent;
                btnThem.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
                btnSua.Visible = frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
                btnXoa.Visible = frmMain.IsPermis(idXoa, main.CurrentPermis.PERMISSION);
                btnLuu.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION) && frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);

            }
        }
        public HoaDonItem()
        {
            InitializeComponent();

        }
        private void RefreshForm(string p)
        {
            txtMa.Text = p;
            txtTen.Text = p;
            txtDonViTinh.SelectedValue = p;
        }
        private DTO.TONHANGInfo GetData(DTO.TONHANGInfo inf)
        {
            inf.MAHANG = txtMa.Text;
            inf.TENHANG = txtTen.Text;
            inf.DVTINH = txtDonViTinh.SelectedValue;
            return inf;
        }
        private void EnabledForm(bool p)
        {
            txtMa.Focus();
            txtMa.Properties.ReadOnly = !p;
            txtTen.Properties.ReadOnly = !p;
            txtDonViTinh.Properties.ReadOnly = !p;
            btnXoa.Enabled = !p;
            btnThem.Enabled = !p;
            btnLuu.Enabled = p;
            btnXoa.Enabled = !p;
            btnSua.Enabled = !p;
            btnBoQua.Enabled = p;
            gridHoaDon.Enabled = !p;
        }
        private void SetData(DTO.TONHANGInfo inf)
        {
            txtMa.Text = inf.MAHANG;
            txtTen.Text = inf.TENHANG;
            txtDonViTinh.SelectedValue = inf.DVTINH;
        }

        private void gvHoaDon_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            int[] currentRows = gvHoaDon.GetSelectedRows();
            if (currentRows != null && currentRows.Length > 0)
            {
                DTO.TONHANGInfo inf = new DTO.TONHANGInfo(gvHoaDon.GetDataRow(currentRows[0]));
                SetData(inf);
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            RefreshForm("");
            EnabledForm(true);

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            EnabledForm(true);
            txtMa.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sErr = "";
            if (txtMa.Text == "")
            {
                XtraMessageBox.Show("Vui lòng nhập mã hàng");
                return;
            }
            BUS.TONHANGControl ctr = new BUS.TONHANGControl();
            DTO.TONHANGInfo inf = new DTO.TONHANGInfo();
            if (txtMa.Enabled)
            {
                int[] currentRows = gvHoaDon.GetSelectedRows();
                if (currentRows != null && currentRows.Length > 0)
                {
                    inf = new DTO.TONHANGInfo(gvHoaDon.GetDataRow(currentRows[0]));
                    inf = GetData(inf);
                    inf.CREATEBY = inf.MODIFIEDBY = frmMain.__userID;
                    inf.CREATEDATE = inf.MODIFIEDDATE = DateTime.Now;
                    sErr = ctr.InsertUpdate(inf);
                    if (sErr == "")
                        XtraMessageBox.Show("Lưu thành công", "Thông báo");
                    else XtraMessageBox.Show(sErr, "Lỗi");
                    EnabledForm(false);
                    LoadHang();
                    return;
                }
            }
            inf = GetData(inf);
            inf.CREATEBY = inf.MODIFIEDBY = frmMain.__userID;
            inf.CREATEDATE = inf.MODIFIEDDATE = DateTime.Now;
            sErr = ctr.InsertUpdate(inf);
            if (sErr == "")
                XtraMessageBox.Show("Lưu thành công", "Thông báo");
            else XtraMessageBox.Show(sErr, "Lỗi");
            LoadHang();
            EnabledForm(false);
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            EnabledForm(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            sErr = "";
            int[] currentRows = gvHoaDon.GetSelectedRows();
            if (currentRows != null && currentRows.Length > 0)
            {
                BUS.TONHANGControl ctr = new BUS.TONHANGControl();
                DTO.TONHANGInfo inf = new DTO.TONHANGInfo(gvHoaDon.GetDataRow(currentRows[0]));
                if (!ctr.IsUsed(inf.MAHANG))
                {
                    if (XtraMessageBox.Show("Bạn có thực sự muốn xóa", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        sErr = ctr.Delete(inf.MAHANG);
                        if (sErr == "")
                            XtraMessageBox.Show("Xóa thành công", "Thông báo");
                        else XtraMessageBox.Show(sErr, "Lỗi");
                        gridHoaDon.DataSource = ctr.GetAll(ref sErr);
                    }
                }
                else
                    XtraMessageBox.Show("Dữ liệu đã được dùng, bạn không thể xóa", "Thông báo");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void HoaDonItem_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);
            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);
            }
           
        }

        private void gridHoaDon_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                if (btnSua.Enabled)
                    btnSua_Click(null, null);
            }
            else if (e.KeyCode == Keys.N && e.Modifiers == Keys.Control)
            {
                if (btnThem.Enabled)
                    btnThem_Click(null, null);
            }
            else if (e.KeyCode == Keys.X && e.Modifiers == Keys.Control)
            {
                if (btnXoa.Enabled)
                    btnXoa_Click(null, null);
            }
        }

        private void HoaDonItem_Load(object sender, EventArgs e)
        {
            EnabledForm(false);
            BUS.DONVITINHControl ctrdvt = new BUS.DONVITINHControl();
            txtDonViTinh.DataSource = ctrdvt.GetAll(ref sErr);
            LoadHang();
            SetPermission();
        }

        private void LoadHang()
        {
            BUS.TONHANGControl ctr = new BUS.TONHANGControl();
            gridHoaDon.DataSource = ctr.GetAll(ref sErr);
        }
    }
}